#ifndef SENSOR_BOS_BMI160_H__
#define SENSOR_BOS_BMI160_H__

/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2025-01-29     甘泽       the first version
 */

#include <rtthread.h>
#include <stm32f4xx_hal.h>

/* 使用BMI160进行加速度和角速度测量 */
#define BMI160_USING_ACCE
#define BMI160_USING_GYRO

/* 使用SPI接口读取BMI160传感器 */
#define BMI160_USING_SPI
#define BMI160_ACCE_NAME "bmi160_acce"
#define BMI160_GYRO_NAME "bmi160_gyro"
#define BMX160_MAG_NAME "bmx160_mag"

#define BMI160_SPI SPI1

#define BMI160_SPI_SCK_GPIO GPIOA
#define BMI160_SPI_SCK_PIN GPIO_PIN_5
#define BMI160_SPI_SCK_AF GPIO_AF5_SPI1

#define BMI160_SPI_MOSI_GPIO GPIOA
#define BMI160_SPI_MOSI_PIN GPIO_PIN_7
#define BMI160_SPI_MOSI_AF GPIO_AF5_SPI1

#define BMI160_SPI_MISO_GPIO GPIOA
#define BMI160_SPI_MISO_PIN GPIO_PIN_6
#define BMI160_SPI_MISO_AF GPIO_AF5_SPI1

#define BMI160_SPI_CS_GPIO GPIOA
#define BMI160_SPI_CS_PIN GPIO_PIN_4

#define BMI160_SPI_SCK_GPIO_RCC_ENABLE() __HAL_RCC_GPIOA_CLK_ENABLE()
#define BMI160_SPI_MISO_GPIO_RCC_ENABLE() __HAL_RCC_GPIOA_CLK_ENABLE()
#define BMI160_SPI_MOSI_GPIO_RCC_ENABLE() __HAL_RCC_GPIOA_CLK_ENABLE()
#define BMI160_SPI_CS_GPIO_RCC_ENABLE() __HAL_RCC_GPIOA_CLK_ENABLE()

#define BMI160_SPI_RCC_ENABLE() __HAL_RCC_SPI1_CLK_ENABLE()

#ifdef BMI160_USING_I2C
#define BMI160_I2C_ADDRESS 0x68 /* 当SDO接到GND为0x68，当SDO接到VCC为0x69 */
#define BMI160_I2C I2C1
#define BMI160_I2C_SCL_GPIO GPIOA
#define BMI160_I2C_SCL_PIN GPIO_PIN_0
#define BMI160_I2C_SCL_AF GPIO_AF4_I2C1

#define BMI160_I2C_SDA_GPIO GPIOA
#define BMI160_I2C_SDA_PIN GPIO_PIN_0
#define BMI160_I2C_SDA_AF GPIO_AF4_I2C1

#define BMI160_I2C_SCL_GPIO_RCC_ENABLE() __HAL_RCC_GPIOA_CLK_ENABLE()
#define BMI160_I2C_SDA_GPIO_RCC_ENABLE() __HAL_RCC_GPIOA_CLK_ENABLE()

#define BMI160_I2C_RCC_ENABLE() __HAL_RCC_I2C1_CLK_ENABLE()

#define BMI160_I2C_ADDR BMI160_I2C_ADDR_PRIM
#endif

enum bmi160_bmm150_power_mode
{
    sensor_power_down,
    sensor_power_normal,
    sensor_power_low
};

enum bmi160_fetch_data_mode
{
    sensor_mode_int,
    sensor_mode_fifo,
    sensor_mode_normal
};

enum bmi160_cmd
{
    get_id,
    set_odr,
    set_range,
    set_powermode,
    set_rdmode
};

/// @brief 注册并启动bmi160
/// @param
/// @return
int start_bmi160(void);

#endif
